package com.example.zhou.connect;

import com.example.zhou.callback.OldNewsCallback;
import com.example.zhou.url.ZhihuUrl;


import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by 11065 on 2016/10/28.
 */

public class OldNewsConnect implements OldNewsInterface {


    @Override
    public void getOldNews(String date, final OldNewsCallback callback) {
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder()
                .url(ZhihuUrl.NewsBefore + date)
                .build();

        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                callback.onOldNewsFail(e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                callback.onOldNewsSuccess(response.body().string());
            }

        });
    }
}

interface OldNewsInterface {
    void getOldNews(String date, OldNewsCallback callback);
}
